<?php
namespace Admin\Controller;
use Common\Controller\BaseController;

class UserController extends BaseController{

	public function _initialize(){
		parent::_initialize();
	}
	/**
	 * 用户登录
	 */
	public function login(){

		$url = I('get.url','','htmlspecialchars');
		$post = I('post.');

		$this->assign('url',$url);
		if(empty($post['user_name'])&&empty($post['password'])){
			$this->display('Public/login');
			exit;
		}
		$user_name = $post['user_name'];
		$password  = $post['password'];
		$verify_image  = $post['verify_image'];
		
		//验证码错误
//		if(!$this->check_verify($verify_image)){
//			header('Content-Type:text/html;charset=utf-8');
//			echo "<script>alert('验证码错误');</script>";
//			$this->display('Public/login');
//			exit;
//		}
		$return = array('status'=>'','info'=>'');
		if(!empty($user_name) && !empty($password)){
			$info = D('Staff')->checkLogin($user_name,$password);
			$url = I('post.url','','htmlspecialchars');

			if(false!==$info){
				session('USER',$info);
				$module_name = 2==$info['is_admin_user'] ? "Home" : "Admin";
				session('module_name',$module_name);
				if(empty($url)){
					redirect(U($module_name.'/Index/index'));
				}else{
					redirect(U(urldecode($url)));
				}
			}else{
				header('Content-Type:text/html;charset=utf-8');
				echo "<script>alert('用户名或密码错误');</script>";
				$this->display('Public/login');
				exit;
			}

		}else{
			header('Content-Type:text/html;charset=utf-8');
			echo "<script>alert('用户名或密码错误');</script>";
			$this->display('Public/login');
			exit;
		}
		exit;
	}
	/**
	 * 退出登录
	 */
	public function logOut(){
		session_destroy();
		redirect(U('Admin/User/login'));
		exit;
	}

	/**
	 * 获取管理员列表
	 */
	public function getAdmin(){

		$name = $this->_get('condition');
		$where = !empty($name) ? " locate(user_name,'{$name}') and del=1": ' del=1';
		$list = D('AdminUsers')->getAdminList($where);
		$this->assign('list',$list);
		$this->display('index');

	}

	/**
	 * 添加管理员,显示添加页面
	 */
	public function add(){

		$this->display('add');

	}
	/**
	 * 修改管理员信息，显示修改页面
	 */
	public function edit(){

		$id = $this->_get('id');

		$id = intval($id);
		$info  = D('AdminUsers')->getInfoById($id);
		if(empty($info)){
			$this->error('数据不存在');
		}
		$this->assign('info',$info);
		$this->display('edit');
	}

	/**
	 * 删除管理员
	 */
	public function delete(){

		$id = $this->_get('id');
		$id = intval($id);
		$info  = D('AdminUsers')->getInfoById($id);
		if(empty($info)){
			$this->error('数据不存在');
		}
		$res = D('AdminUsers')->where('id='.$id)->delete();
		if(false !==$res){
			$this->success('删除成功',U('/User/getAdmin/'));
		}else{
			$this->error('删除失败');
		}
	}
	/**
	 * 保存添加修改数据
	 */
	public function save(){
		$data = $this->_post();

		$res  = D('AdminUsers')->saveAdmins($data);

		if($res['status']){
			$this->success($res['info'],U('/User/getAdmin/'));
		}else{
			$this->error($res['info']);
		}
	}

	/**
	 * 修改密码
	 */
	public function pass(){
		$data = $this->_post();

		if(empty($data)){
			$this->display('pass');
			exit;
		}

		$res  = D('Users')->changePass($data);

		if($res['status']){
			$this->success($res['info']);
		}else{
			$this->error($res['info']);
		}
	}

	public function create_verify(){
		$config =    array(
			'fontSize'    =>    10,    // 验证码字体大小
			'length'      =>    4,     // 验证码位数
			'useNoise'    =>    false, // 关闭验证码杂点
			'imageW'      =>    69,  //验证码宽度
			'imageH'      =>    27,  //
			'codeSet'     =>    "0123456789",  //验证码字符集合
			'useCurve'    =>    false,  //是否使用混淆曲线 默认为true
			'expire'      =>    60*60,  //是否使用混淆曲线 默认为true
//			'useImgBg'    =>    true,  //是否使用背景图片 默认为false
		);
		$Verify = new \Think\Verify($config);
		$Verify->entry();
	}
	/**
	 * @param $code
	 * @param string $id
	 * @return bool
	 * 验证验证码
	 */
	public function check_verify($code, $id = ''){
		$verify = new \Think\Verify();
		return $verify->check($code, $id);
	}
}
